package com.apotik.legenda.dao;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.apotik.legenda.model.Role;
import com.apotik.legenda.model.User;
import com.apotik.legenda.util.AppUtil;
import com.apotik.legenda.util.HibernateUtil;

public class UserDao {

	public UserDao() {
		// TODO Auto-generated constructor stub
	}
	
	public boolean saveOrUpdate(User obj){
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			//session.save(obj);
			session.saveOrUpdate(obj);
			session.getTransaction().commit();
			session.close();
			return true;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public User login(String username, String password) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		//session.beginTransaction();
		
		Criteria cr = session.createCriteria(User.class);
		cr.add(Restrictions.eq("userName", username));
		//cr.add(Restrictions.eq("password", password));
		
		List<User> users = cr.list();
		
		if(users.size()>0){
			session.close();
			return users.get(0);
		}
		
		session.close();
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> loadCashiersByActive(boolean isActive){
		List<User> users = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			RoleDao roleDao = new RoleDao();
			Role role = roleDao.getRole(AppUtil.ROLE_KASIR);
			if(role!=null){
				Criteria cr = session.createCriteria(User.class);
				cr.add(Restrictions.eq("isActive", isActive));
				cr.add(Restrictions.eq("role", role));
				cr.addOrder(Order.asc("fullName"));
				
				users = cr.list();
			}
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return users;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> search(String codeName){
		List<User> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria cr = session.createCriteria(User.class);
			cr.add(Restrictions.ilike("fullName", "%"+codeName+"%"));
			cr.add(Restrictions.ilike("userName", "%"+codeName+"%"));
			//System.out.println(codeName);
			list = cr.list();
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
		
	}
	
	@SuppressWarnings("unchecked")
	public String getDataRole(String roleCode){
		String roleName = "";
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Role.class);
			cr.add(Restrictions.eq("roleCode", roleCode));
			
			List<Role> list = cr.list();
			if(list.size()>0){
				
			}
			
		} catch (Exception e) {
			System.out.println("Exception: "+e.getMessage());
		}
		
		return roleName;
	}
	
	public boolean delete(User obj){
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.delete(obj);
			session.getTransaction().commit();
			session.close();
			return true;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}
	
}
